/**
 * js
 */
angular.module('mall.payment',[
    "ngStorage"
])

.config(['$stateProvider', '$urlRouterProvider', 
function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('morepay', {
            url: '/morepay',
            templateUrl: cmt_mall_templateprefix+'/pay/morepay.html',
            params:{
                order:null
            },
            controller: 'MorepayController'
        })
    ;
}])

//支付页面控制器
.controller('MorepayController', ['$scope','$state','$window','$stateParams','OrderService','AliWapPay','WxpayService','ShopcartService',
    function($scope, $state, $window, $stateParams,OrderService,AliWapPay,WxpayService,ShopcartService){

        $scope.environment = false;
        //判断是否满足微信支付环境
        if (typeof WeixinJSBridge != "undefined") {
            $scope.environment = true;
        }
        //paymentpassword();//支付密码输入框设置
        $scope.mall_enturl = projectBaseUrl;//回跳地址
        ShopcartService.synchronizationStorage();
        //OrderService.delcache(); //清除缓存的订单信息
       /*MallAuthService.Userbalance().$promise.then(function (data) {
            $scope.paysecret = data.paysecret;
        }, function (error) {
            //空
        })*/
        $scope.order = null;
        $scope.way = "";
        //选择支付方式
        function choose_pay() {
            var payWay = $('input:radio:checked').val();
            if (payWay == 1) {
                $scope.way = 'wx';
            } else if (payWay == 2) {
                $scope.way = 'ali';
            } else if (payWay == 3) {
                $scope.way = 'yue';
            }
        }

        //默认支付方式
        if ($scope.environment) {
            $('#paythree1').attr("checked", "checked")
        } else {
            $('#paythree2').attr("checked", "checked")
        }

        //获取订单支付信息。
        if ($stateParams.order != null) {
            $scope.order = OrderService.getOrderById($stateParams.order.orderid);
            OrderService.cacheOrderid($stateParams.order.orderid);
        } else if ($stateParams.orderid != '' && $stateParams.order == null) {
            $scope.order = OrderService.getOrderById($stateParams.orderid);
            OrderService.cacheOrderid($stateParams.orderid);
        } else {
            var orderid = OrderService.getCacheOrederid();
            $scope.order = OrderService.getOrderById(orderid);
        }
        //确认下单
        $scope.place_order = function () {
            choose_pay();
            if ($scope.way == "") {
                alert("请选择支付方式");
            } else {
                if (!$scope.order.buyMessage) {
                    $scope.order.buyMessage = "农归吧订单";
                } else if ($scope.way == "yue") {
                    if ($scope.paysecret == null || $scope.paysecret == '') {
                        input_alert("支付密码未设置", 1000);
                        setTimeout(function () {
                            $window.location.href = projectBaseUrl + "setpassword";
                        }, 1000);
                    } else {
                        $(".payment-pages").css({opacity:1,zIndex:1});
                        $(".payment_keyboard").css({transform: "translateY(0px)"}, {"-webkit-transform": "translateY(0px)"});
                    }
                }
                $scope.wapPay($scope.order);
            }
        }
        //支付调用
        $scope.wapPay = function (order_info) {
            if ($scope.way == "wx") {//微信支付
                if (appEnvironment == false) {
                    function success(result) {
                        WeixinJSBridge.invoke('getBrandWCPayRequest', {
                            "appId": result.appID,//"wx2421b1c4370ec43b", //公众号名称，由商户传入
                            "timeStamp": result.timeStamp,//"1395712654", //时间戳，自1970年以来的秒数
                            "nonceStr": result.nonceStr,//"e61463f8efa94090b1f366cccfbbb444", //随机串
                            "package": result.pacKage,//"prepay_id=u802345jgfjsdfgsdg888",
                            "signType": result.signType,//"MD5", //微信签名方式:
                            "paySign": result.paySign,//"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
                        }, function (res) { // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                            if (res.err_msg == "get_brand_wcpay_request:ok") {
                                OrderService.delcacheid();
                                $window.location.href = projectBaseUrl + "orderlist/2";
                            }
                            if (res.err_msg == "get_brand_wcpay_request:cancel") {
                                alert("交易取消");
                            }
                            if (res.err_msg == "get_brand_wcpay_request:fail") {
                                alert("支付失败");
                            }
                        });
                    }

                    WxpayService.jspiPay(order_info, success);
                }
                else if (appEnvironment == true) {
                    function success_func(result) {
                        Wechat.sendPaymentRequest({
                                "appid": result.appID,//"wx2421b1c4370ec43b", //公众号名称，由商户传入
                                "partnerid": result.partnerid,
                                "prepayid": result.prepayid,
                                "timestamp": result.timeStamp,//"1395712654", //时间戳，自1970年以来的秒数
                                "noncestr": result.nonceStr,//"e61463f8efa94090b1f366cccfbbb444", //随机串
                                "package": result.pacKage,//"prepay_id=u802345jgfjsdfgsdg888",
                                "sign": result.paySign,//"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
                            }, function (res) { // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                                if (res.err_msg == "get_brand_wcpay_request:ok") {
                                    OrderService.delcacheid();
                                    $window.location.href = projectBaseUrl + "orderlist/2";
                                }
                                if (res.err_msg == "get_brand_wcpay_request:cancel") {
                                    alert("交易取消");
                                }
                                if (res.err_msg == "get_brand_wcpay_request:fail") {
                                    alert("支付失败");
                                }
                            },
                            function (reason) {
                                console.log(JSON.stringify(reason));
                            }
                        );
                    }

                    function error(result) {
                        console.log(result);
                    }

                    WxpayService.appPay(order_info, success_func, error);
                }
            }
            if ($scope.way == "ali") {//阿里支付
                if (appEnvironment == false) {
                    AliWapPay.webPay(order_info).then(function (data) {
                        $window.location.href = data;
                    });
                } else if (appEnvironment == true) {
                    var order_info = order_info;
                    AliWapPay.aliAppPay(order_info, function (reason) {
                        AliWapPay.AppPay(reason, order_info);
                    }, function (reason) {
                        console.log(reason)
                    })
                }
            }
        }
}])
;
